package com.quectel.core.module.alarm.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.alarm.dao.AlarmLinkageRuleDao;
import com.quectel.core.module.alarm.dto.AlarmLinkageRuleDto;
import com.quectel.core.module.alarm.entity.AlarmLinkageRuleEntity;
import com.quectel.core.module.alarm.service.AlarmEventConfigService;
import com.quectel.core.module.alarm.service.AlarmLinkageRuleService;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-10-19 13:39:29
 */
@DubboService
public class AlarmLinkageRuleServiceImpl implements AlarmLinkageRuleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AlarmLinkageRuleServiceImpl.class);

    @Autowired
    private AlarmLinkageRuleDao alarmLinkageRuleDao;
    @Autowired
    private AlarmEventConfigService alarmEventConfigService;
    @Autowired
    private DeviceService deviceService;


    @Override
    public List<AlarmLinkageRuleDto> selectCacheByDeviceIdEventConfigId(Long deviceId, Long alarmEventConfigId) {
        return CacheKit.cacheToRedis(
                () -> {
                    List<AlarmLinkageRuleEntity> entityList = alarmLinkageRuleDao.selectList(new LambdaQueryWrapper<AlarmLinkageRuleEntity>()
                            .eq(AlarmLinkageRuleEntity::getDeviceId, deviceId)
                            .eq(AlarmLinkageRuleEntity::getAlarmEventConfigId, alarmEventConfigId)
                    );
                    return CopyUtils.copyList(entityList, AlarmLinkageRuleDto.class);
                },
                RedisCacheConstants.DEVICE_ALARM_EVENT_CONFIGS + "deviceId:" + deviceId + "alarmEventConfigId:" + alarmEventConfigId,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public List<AlarmLinkageRuleDto> queryList(Map<String, Object> params) {
        List<AlarmLinkageRuleEntity> list = alarmLinkageRuleDao.queryList(params);
        List<AlarmLinkageRuleDto> result = CopyUtils.copyList(list, AlarmLinkageRuleDto.class);
        result.forEach(v -> padding(v));
        return result;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return alarmLinkageRuleDao.queryTotal(params);
    }

    @Override
    public Long save(AlarmLinkageRuleDto dto) {
        AlarmLinkageRuleEntity entity = CopyUtils.copyObj(dto, AlarmLinkageRuleEntity.class);
        entity.setId(Snowflake.nextId());
        alarmLinkageRuleDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(AlarmLinkageRuleDto dto) {
        AlarmLinkageRuleEntity entity = CopyUtils.copyObj(dto, AlarmLinkageRuleEntity.class);
        alarmLinkageRuleDao.updateById(entity);
    }

    private static String getColumnName(String fieldName) {
        Field[] declaredFields = AlarmLinkageRuleEntity.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            if (declaredField.getName().equals(fieldName)) {
                TableField annotation = declaredField.getAnnotation(TableField.class);
                if (annotation != null && StringUtils.isNotBlank(annotation.value())) {
                    return annotation.value();
                }
                return StrUtil.toUnderlineCase(declaredField.getName());
            }
        }
        return null;
    }

    @Override
    public AlarmLinkageRuleDto selectById(Long id) {
        AlarmLinkageRuleEntity entity = alarmLinkageRuleDao.selectById(id);
        return padding(CopyUtils.copyObj(entity, AlarmLinkageRuleDto.class));
    }

    private AlarmLinkageRuleDto padding(AlarmLinkageRuleDto alarmLinkageRuleDto) {
        if (alarmLinkageRuleDto.getDeviceId() != null) {
            alarmLinkageRuleDto.setAlarmDeviceDto(deviceService.selectById(alarmLinkageRuleDto.getDeviceId()));
        }
        if (alarmLinkageRuleDto.getLinkageDeviceId() != null) {
            alarmLinkageRuleDto.setLinkageDeviceDto(deviceService.selectById(alarmLinkageRuleDto.getLinkageDeviceId()));
        }
        if (alarmLinkageRuleDto.getAlarmEventConfigId() != null) {
            alarmLinkageRuleDto.setAlarmEventConfigDto(alarmEventConfigService.selectById(alarmLinkageRuleDto.getAlarmEventConfigId()));
        }
        return alarmLinkageRuleDto;
    }

    @Override
    public void deleteById(Long id) {
        alarmLinkageRuleDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
